package com.zy.controller;

import com.zy.pojo.User;
import com.zy.service.JurisdictionService;
import com.zy.service.UserService;
import com.zy.vo.ResultVo;
import com.zy.vo.TreeNode;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
public class ListController {
    @Resource
    private UserService userService;
    @Resource
    private JurisdictionService jurisdictionService;

    @GetMapping("/list")
    @RequiresPermissions("user:list")
    public ResultVo getlist(){
        return ResultVo.sucess(userService.getUserList());
    }

    //添加
    @PostMapping("/add")
    @RequiresPermissions("user:add")
    public ResultVo add(@RequestBody User user){
        try {
            user.setBirthday(new Date());
            userService.add(user);
            return ResultVo.sucess("用户添加成功");
        }catch (Exception e){
            return ResultVo.fail("用户添加失败",e);  //包含了打印错误日志
        }
    }

    //删除
    @GetMapping("/del")
    @RequiresPermissions("user:del")
    public ResultVo del(String id){
        try {
            userService.delete(Integer.parseInt(id));
            return ResultVo.sucess("用户删除成功");
        }catch (Exception e){
            return ResultVo.fail("用户删除失败",e);  //包含了打印错误日志
        }
    }

    //更新
    @PostMapping("/update")
    @RequiresPermissions("user:update")
    public ResultVo update(@RequestBody User user){
        try {
//            user.setBirthday(new Date());
            userService.update(user);
            return ResultVo.sucess("用户更新成功");
        }catch (Exception e){
            return ResultVo.fail("用户更新失败",e);  //包含了打印错误日志
        }
    }


    @GetMapping("/grant")
    @RequiresPermissions("user:grant")
    public ResultVo grant(Integer userId){
        List<TreeNode> list = jurisdictionService.getTreeNodes(userId);
        return ResultVo.sucess(list);
    }
    @GetMapping("/dogrant")
    public ResultVo dogrant(Integer userId,Integer[] nid){
        try{
            jurisdictionService.dogrant(userId,nid);
            return ResultVo.sucess("授权成功");
        }catch (Exception e){
            return ResultVo.fail("授权失败",e);
        }

    }
}
